// pages/home/home.js
const db = wx.cloud.database()

const icon = db.collection('icon')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: [],
    news: [],
    newsArticle: [],
    officialNews: [],
    banner: []
  },

  showCovidInfo: function () {
    wx.navigateTo({
      url: '/pages/covid/covid',
    })
  },

  getNewsArticle: function () {
    var that = this
    const db = wx.cloud.database()
    db.collection('news_article').orderBy('_updateTime','desc').limit(5).get().then(res => {
      console.log(res.data)
      that.setData({
        newsArticle: res.data
      })
    })
  },

  getOfficialNews: function () {
    var that = this
    const db = wx.cloud.database()
    db.collection('officialInfo').orderBy('_updateTime','desc').limit(5).get().then(res => {
      console.log(res.data)
      that.setData({
        officialNews: res.data
      })
    })
  },

  showAllNews: function (e) {
    console.log(e.currentTarget.dataset.db)
    if(e.currentTarget.dataset.db == "officialInfo") {
      wx.navigateTo({
        url: '/pages/allOfficial/allOfficial',
      })
    } else if(e.currentTarget.dataset.db == "news_article") {
      wx.navigateTo({
        url: '/pages/allNews/allNews?id=0',
      })
    }
  },

  showDetail: function (e) {
    if(e.currentTarget.dataset.id){
      var id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: '/pages/detail/detail?id=' + id,
      })
    }
  },

  showOfficialDetail:function(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/official/official?id=' + id,
    })
  },

  showCase:function(e){
    var id = e.currentTarget.dataset.id
    console.log(id)
    wx.navigateTo({
      url: '/pages/casedetail/casedetail?id=' + id,
    })
  },


  getBanner: function () {
    var _this = this
    const db = wx.cloud.database()
    const news = db.collection('banner')
    news.get({
      success(res) {
        console.log(res.data)
        _this.setData({
          banner: res.data
        })
      }
    })
  },

  getHot: function () {
    var that = this
    wx.cloud.callFunction({
      name: 'getHotTitle'
    }).then(res=>{
      console.log(res.result)
      that.setData({
        title: res.result
      })
    })
  },


  onLoad: function (options) {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getBanner()
    this.getHot()
    this.getNewsArticle()
    this.getOfficialNews()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})